package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.SeekBar;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.stark.ve.EditorHolder;
import com.stark.ve.core.IEditorListener;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.databinding.ActivityVideoRotateBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.FileP2pUtil;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.MediaUtil;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.TimeUtil;

public class VideoRotateActivity extends BaseAc<ActivityVideoRotateBinding> {

    public static String videoPath;
    private boolean isExperience = false;
    private int angle = 90;
    private boolean isFlip;
    private long videoLength;
    private Handler mHandler;
    private final Runnable mTaskUpdateTime = new Runnable() {
        @Override
        public void run() {
            mDataBinding.tvVideoLeftText.setText(TimeUtil.getMmss(mDataBinding.videoView.getCurrentPosition()));
            mHandler.postDelayed(this, 500);
        }
    };

    @Override
    protected int onCreate() {
        return R.layout.activity_video_rotate;
    }

    @Override
    protected void initView() {
        mDataBinding.ivVideoRotateBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivVideoRotateExperience.setOnClickListener(this);
        mDataBinding.ivVideoPlay.setOnClickListener(this);
        mDataBinding.ivVideoRotateSave.setOnClickListener(this);

        mDataBinding.ivRotate90.setOnClickListener(this);
        mDataBinding.ivRotate180.setOnClickListener(this);
        mDataBinding.ivRotate270.setOnClickListener(this);
        mDataBinding.ivFlip.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivRotate90://90
                clearSelection();
                angle = 90;
                mDataBinding.ivRotate90.setImageResource(R.drawable.xz90);
                break;
            case R.id.ivRotate180://180
                clearSelection();
                angle = 180;
                mDataBinding.ivRotate180.setImageResource(R.drawable.xz180a);
                break;
            case R.id.ivRotate270://270
                clearSelection();
                angle = 270;
                mDataBinding.ivRotate270.setImageResource(R.drawable.xz270a);
                break;
            case R.id.ivFlip://镜像
                if (isFlip) {
                    isFlip = false;
                    mDataBinding.ivFlip.setImageResource(R.drawable.jx);
                } else {
                    isFlip = true;
                    mDataBinding.ivFlip.setImageResource(R.drawable.jxa);
                }
                break;
            case R.id.ivVideoPlay://播放/暂停
                if (mDataBinding.videoView.isPlaying()) {
                    mDataBinding.ivVideoPlay.setImageResource(R.drawable.bf);
                    mDataBinding.videoView.pause();
                    stopTime();
                } else {
                    mDataBinding.ivVideoPlay.setImageResource(R.drawable.icon_zt);
                    mDataBinding.videoView.start();
                    startTime();
                }
                break;
            case R.id.ivVideoRotateExperience://立即体验
                if (mDataBinding.videoView.isPlaying()) {
                    mDataBinding.ivVideoPlay.setImageResource(R.drawable.bf);
                    mDataBinding.videoView.pause();
                }
                //旋转
                rotateVideo();
                break;
            case R.id.ivVideoRotateSave://导出
                if (!isExperience) {
                    ToastUtils.showShort(getString(R.string.please_rotate_video));
                    return;
                } else {
                    if (mDataBinding.videoView.isPlaying()) {
                        mDataBinding.ivVideoPlay.setImageResource(R.drawable.bf);
                        mDataBinding.videoView.pause();
                        stopTime();
                    }
                    saveVideo();
                }
                break;
        }
    }

    @Override
    protected void initData() {
        if (TextUtils.isEmpty(videoPath)) return;
        videoLength = MediaUtil.getDuration(videoPath);
        setPlayer();
    }

    //保存视频
    private void saveVideo() {
        showDialog(getString(R.string.saving));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        FileP2pUtil.copyPrivateVideoToPublic(mContext, videoPath);
                        String filePath = FileUtil.generateFilePath(Content.myRecord, "." + FileUtils.getFileExtension(videoPath));
                        FileUtils.copy(videoPath, filePath);
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        ToastUtils.showShort(R.string.save_success);
                        startActivity(HomeActivity.class);
                    }
                });
            }
        }, 1000);
    }

    //开始旋转
    private void rotateVideo() {
        EditorHolder.editor().videoRotate(videoPath, angle, isFlip, new IEditorListener() {
            @Override
            public void onSuccess(String outFilePath) {
                dismissDialog();
                isExperience = true;
                videoPath = outFilePath;
                mDataBinding.videoView.setVideoPath(videoPath);
                mDataBinding.videoView.seekTo(1);
                mDataBinding.ivVideoPlay.setImageResource(R.drawable.icon_zt);
                mDataBinding.videoView.start();
            }

            @Override
            public void onFailure(String errMsg) {
                dismissDialog();
                ToastUtils.showShort(R.string.rotate_def);
            }

            @Override
            public void onProgress(int progress) {
                showDialog(getString(R.string.rotate_ing) + progress + "%");
            }
        });
    }

    //设置播放器
    private void setPlayer() {
        mHandler = new Handler();
        //设置seekbar
        mDataBinding.tvVideoLeftText.setText("00:00");
        mDataBinding.tvVideoRightText.setText("/" + TimeUtil.getMmss(videoLength));
        //设置播放器
        mDataBinding.videoView.setVideoPath(videoPath);
        mDataBinding.videoView.seekTo(1);
        mDataBinding.videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {  // 播放完成的监听
                mDataBinding.tvVideoLeftText.setText("00:00");
                mDataBinding.tvVideoRightText.setText("/" + TimeUtil.getMmss(videoLength));
                mDataBinding.ivVideoPlay.setImageResource(R.drawable.bf);
                mp.seekTo(1);
                stopTime();
            }
        });
        //开始播放视频
        mDataBinding.ivVideoPlay.setImageResource(R.drawable.icon_zt);
        mDataBinding.videoView.start();
        startTime();
    }

    private void startTime() {
        mHandler.post(mTaskUpdateTime);
    }

    private void stopTime() {
        mHandler.removeCallbacks(mTaskUpdateTime);
    }

    @Override
    protected void onStop() {
        super.onStop();
        mDataBinding.ivVideoPlay.setImageResource(R.drawable.bf);
        mDataBinding.videoView.pause();
        stopTime();
    }

    private void clearSelection() {
        mDataBinding.ivRotate90.setImageResource(R.drawable.xz_90a);
        mDataBinding.ivRotate180.setImageResource(R.drawable.xz180);
        mDataBinding.ivRotate270.setImageResource(R.drawable.xz270);
        mDataBinding.ivFlip.setImageResource(R.drawable.jx);
    }

}